﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

/*********************************************
 * TODO: Test the Functionality of this class
 *********************************************/
namespace RZEngine2D
{
    /*
     * This is teh Base Message class that ALL message should(but dont need to) derive from
     */
    public abstract class BaseMessage
    {
        uint msgID;
        public BaseMessage(uint MSGID)
        {
            msgID = MSGID;
        }

        public uint MessageID
        {
            get { return msgID; }
        }
    }

    public class RZMessageSystem
    {
        // the user defined delegate for handling messages
        private delegate void MessageFunc(BaseMessage baseMessage);
        MessageFunc MessageProc;

        // hold all the messages
        Queue<BaseMessage> MsgQueue = new Queue<BaseMessage>();

        // singleton
        private RZMessageSystem() { }
        private RZMessageSystem(RZMessageSystem ms) {}

        RZMessageSystem instance = new RZMessageSystem();
        public RZMessageSystem Instance
        {
            get { return instance; }
        }

        // Main functions for use
        void InitMessageSystem(MessageFunc func)
        {
            if (func == null)
                return;
            MessageProc = func;
        }

        void SendMessage(BaseMessage msg)
        {
            if (msg == null)
                return; // TODO: Throw and Exception

            MsgQueue.Enqueue(msg);
        }

        void ProcessMessages()
        {
            // make we have our functor
            if (MessageProc == null)
                return; // TODO: Throw an exception

            // go through the queue of messages and pop the top
            while(MsgQueue.Count > 0)
            {
                MessageProc(MsgQueue.Dequeue());
            }
            MsgQueue.Clear();
        }
        
        void ClearMessages()
        {
            while (MsgQueue.Count > 0)
            {
                MsgQueue.Dequeue();
            }
        }
        
        void ShutdownMessage()
        {
            ClearMessages();
            MessageProc = null;
        }
        


    }
}
